
#include "th_list.h"




tlist init()
{
	tlist tmp=(tlist)malloc(sizeof(t_list));
	if (tmp==NULL) {	printf("allocation error\n");
						exit(-1);}
	tmp->first=tmp->last=NULL;
	return tmp;
}

/*
returns 1 - on success
*/
int enqueue(tlist list, uthread_id tid)
{
	data tmp=(data)malloc(sizeof(data_t));
	if (tmp==NULL)
		{	printf("allocation error\n");
						exit(-1);}
	tmp->id=tid;
	tmp->next=NULL;
	if (list->first == NULL)
		list->last=list->first=tmp;
	else
		list->last=(list->last)->next=tmp;

	return 1;
}



uthread_id dequeue(tlist list)
{
	data tmp;
	uthread_id id;
	if (list->first != NULL)
	{
		tmp = list->first;
		list->first = list->first->next;
		id = tmp->id;
		free(tmp);

		return id;
	}

	list->last = NULL;
	return -1; /* list is empty */
}


void destroy(tlist list)
{
	data tmp;
	if(list == NULL ||list->first==NULL)
		return;
	while (list->first)
	{
		tmp=list->first;
		list->first=list->first->next;
		free(tmp);
	}
	free(list);
}


